package com.bsb.biz.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.util.IdGen;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbPolicy;
import com.bsb.base.model.BsbPolicyExample;
import com.bsb.base.model.BsbPolicyExample.Criteria;
import com.bsb.biz.service.BsbPolicyService;

@Controller
@RequestMapping("/bsbPolicy")
public class BsbPolicyController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbPolicy/list.do";
	
	@Resource
	private BsbPolicyService bsbPolicyService;
	
	@RequiresPermissions("bsbPolicy:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,String title,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbPolicyExample bsbPolicyExample = new BsbPolicyExample();
		Criteria criteria = bsbPolicyExample.createCriteria();
		if(StringUtils.isNotEmpty(title)){
			criteria.andTitleLike("%"+title+"%");
		}
		int totalCount = bsbPolicyService.countBsbPolicy(bsbPolicyExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbPolicyExample.setPageIndex((pageNum - 1) * pageSize);
		bsbPolicyExample.setPageSize(pageSize);
		bsbPolicyExample.setOrderByClause("createDate DESC");
		List<BsbPolicy> bsbPolicys = bsbPolicyService.getPageBsbPolicy(bsbPolicyExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbPolicys", bsbPolicys);
		model.addAttribute("title", title);
		return "/BsbPolicy/list";
	}
	
	@RequiresPermissions("bsbPolicy:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbPolicy/add";
	}

	@RequiresPermissions("bsbPolicy:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model,RedirectAttributes redirectAttributes,@ModelAttribute("bsbPolicy") BsbPolicy bsbPolicy) {
		bsbPolicy.setUuid(IdGen.uuid());
		bsbPolicy.setCreateDate(new Date());
		bsbPolicy.setCreateBy((String)SecurityUtils.getSubject().getPrincipal());
		bsbPolicyService.addBsbPolicy(bsbPolicy);
		redirectAttributes.addFlashAttribute("msg", "新增成功");
		return redirect;
	}
	
	@RequiresPermissions("bsbPolicy:delete")
	@RequestMapping(value = "/{uuid}/delete", method = RequestMethod.GET)
	public String del(Model model,RedirectAttributes redirectAttributes,
			@PathVariable("uuid") String uuid) {
		bsbPolicyService.delBsbPolicyByUuid(uuid);
		redirectAttributes.addFlashAttribute("msg", "删除成功");
		return redirect;
	}
	
	@RequiresPermissions("bsbPolicy:delete")
	@RequestMapping(value = "/{uuid}/hide", method = RequestMethod.GET)
	public String hide(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,@PathVariable("uuid") String uuid) {
		BsbPolicy bsbPolicy = new BsbPolicy();
		bsbPolicy.setUuid(uuid);
		bsbPolicy.setDelFlag(Global.HIDE);
		bsbPolicyService.updateBsbPolicy(bsbPolicy);
		String queryString = request.getQueryString();
		redirectAttributes.addFlashAttribute("msg", "隐藏成功");
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbPolicy:delete")
	@RequestMapping(value = "/{uuid}/show", method = RequestMethod.GET)
	public String show(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,@PathVariable("uuid") String uuid) {
		BsbPolicy bsbPolicy = new BsbPolicy();
		bsbPolicy.setUuid(uuid);
		bsbPolicy.setDelFlag(Global.SHOW);
		bsbPolicyService.updateBsbPolicy(bsbPolicy);
		String queryString = request.getQueryString();
		redirectAttributes.addFlashAttribute("msg", "显示成功");
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbPolicy:update")
	@RequestMapping(value = "/{uuid}/update", method = RequestMethod.GET)
	public String toEdit(Model model,HttpServletRequest request,
			@PathVariable("uuid") String uuid) {
		BsbPolicy bsbPolicy = bsbPolicyService.getBsbPolicyByUuid(uuid);
		model.addAttribute("bsbPolicy", bsbPolicy);
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		return "/BsbPolicy/edit";
	}

	@RequiresPermissions("bsbPolicy:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model,
			@RequestParam(value = "queryString", required = false) String queryString,
			RedirectAttributes redirectAttributes, @ModelAttribute("bsbPolicy") BsbPolicy bsbPolicy) {
		bsbPolicy.setUpdateDate(new Date());
		bsbPolicy.setUpdateBy((String)SecurityUtils.getSubject().getPrincipal());
		bsbPolicyService.updateBsbPolicy(bsbPolicy);
		redirectAttributes.addFlashAttribute("msg", "修改成功");
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbPolicy:view")
	@RequestMapping(value = "/{uuid}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("uuid") String uuid) {
		BsbPolicy bsbPolicy = bsbPolicyService.getBsbPolicyByUuid(uuid);
		model.addAttribute("bsbPolicy", bsbPolicy);
		return "/BsbPolicy/view";
	}
}
